<style>
  .picImg {
    width: 40px;
    height: 40px;
  }
</style>
<template>
  <div class="app-container">
    <!--&lt;!&ndash; 查询和其他操作 &ndash;&gt;-->
    <div class="filter-container">
      <el-button class="filter-item" type="primary" icon="el-icon-edit"
                 @click="handleAdd">新增
      </el-button>
    </div>

    <!-- 查询结果 -->
    <el-table v-loading="listLoading" :data="list" element-loading-text="正在查询中。。。" border fit highlight-current-row
              :header-cell-style="{background:'#92D3FF',color:'#fff'}">

      <!--<el-table-column fixed align="center" min-width="100" label="公司ID" prop="ID"/>-->
      <el-table-column fixed align="center" min-width="100" label="公司名称" prop="companyName"/>
      <el-table-column align="center" min-width="150" label="公司全称" prop="fullName"/>
      <el-table-column align="center" min-width="150" label="统一社会信用代码" prop="bankAccName"/>
      <el-table-column align="center" min-width="100" label="所在省份" prop="province"/>
      <el-table-column align="center" min-width="100" label="所在城市" prop="city"/>
      <el-table-column align="center" min-width="200" label="办公地址" prop="address"/>
      <el-table-column align="center" min-width="150" label="注册地址" prop="registeAddress"/>
      <el-table-column align="center" min-width="100" label="联系人" prop="linkMan"/>
      <el-table-column align="center" min-width="110" label="联系人电话" prop="linkPhone"/>
      <el-table-column align="center" min-width="110" label="手机号" prop="linkMobile"/>
      <el-table-column align="center" min-width="200" label="电子邮箱" prop="email"/>
      <el-table-column align="center" min-width="100" label="登录主账户" prop="userName"/>
      <el-table-column align="center" min-width="200" label="备注" prop="remark"/>
      <el-table-column align="center" min-width="170" label="创建时间" prop="timeAdd"/>
      <el-table-column fixed="right" align="center" label="操作" width="250" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button type="success" size="mini"
                     @click="handleCheck(scope.row)">账户
          </el-button>
          <el-button type="primary" size="mini"
                     @click="handleEdit(scope.row)">编辑
          </el-button>
          <el-popover
            placement="top"
            width="200"
            trigger="manual"
            :ref="'popover-' + scope.row.ID">
            <p>确定删除该数据吗？</p>
            <div style="text-align: right; margin: 0">
              <el-button size="mini" type="text" @click="$refs[`popover-` + scope.row.ID].doClose()">取消</el-button>
              <el-button type="primary" size="mini" @click="doDelete(scope.row)">确定</el-button>
            </div>
            <el-button style="margin-left: 10px;" type="danger" size="mini" slot="reference" @click="$refs[`popover-` + scope.row.ID].doShow()">删除</el-button>
          </el-popover>
        </template>
      </el-table-column>
    </el-table>
    <pagination v-show="total>0" :total="total" :page.sync="listQuery.pageNum" :limit.sync="listQuery.pageSize"
                @pagination="getList"/>
    <!-- 账户列表对话框 -->
    <el-dialog :visible.sync="acclistDialogVisible" title="账户列表"  width="1000px" :close-on-click-modal="false">
      <div class="filter-container">
        <el-button class="filter-item" type="primary" icon="el-icon-edit"
                   @click="handleChildAdd">新增
        </el-button>
      </div>
      <el-table v-loading="acclistLoading" :data="acclist" element-loading-text="正在查询中。。。" border fit highlight-current-row
                :header-cell-style="{background:'#92D3FF',color:'#fff'}">
        <!--<el-table-column fixed align="center" min-width="100" label="公司ID" prop="id"/>-->
        <!--<el-table-column align="center" min-width="80" label="银行代码" prop="bankCode"/>-->
        <el-table-column align="center" min-width="100" label="开户行名称" prop="interBankName"/>
        <el-table-column align="center" min-width="120" label="账户号" prop="account"/>
        <el-table-column align="center" min-width="120" label="账户名称" prop="accName"/>
        <el-table-column align="center" min-width="100" label="注册地址" prop="registeAddress"/>
        <el-table-column align="center" min-width="120" label="联系电话" prop="bankLinkMobile"/>
        <el-table-column align="center" label="操作" width="150" class-name="small-padding fixed-width">
          <template slot-scope="scope">
            <el-button type="primary" size="mini"
                       @click="ChandEdit(scope.row)">编辑
            </el-button>
            <el-popover
              placement="top"
              width="200"
              trigger="manual"
              :ref="'popover-' + scope.row.bankCode">
              <p>确定删除该数据吗？</p>
              <div style="text-align: right; margin: 0">
                <el-button size="mini" type="text" @click="$refs[`popover-` + scope.row.bankCode].doClose()">取消</el-button>
                <el-button type="primary" size="mini" @click="doChildDelete(scope.row)">确定</el-button>
              </div>
              <el-button style="margin-left: 10px;" type="danger" size="mini" slot="reference" @click="$refs[`popover-` + scope.row.bankCode].doShow()">删除</el-button>
            </el-popover>
          </template>
        </el-table-column>
      </el-table>
      <!--增加公司账户对话框-->
      <el-dialog :visible.sync="acclistDialogChildVisible" :title="accountStatus == 'edit' ? '新增账户' : '编辑账户'"
                  width="40%" append-to-body :close-on-click-modal="false">
        <el-form ref="editChildForm" :rules="editChildFormRules" :model="editChildForm" status-icon label-position="left"
                 label-width="120px" style="width: 80%">
          <!--<el-form-item label="银行代码" prop="selectedBankInfo">
            <el-cascader style="width: 100%"
              :options="bankInfo"
              v-model="editChildForm.selectedBankInfo"
              @active-item-change="handleItemChange">
            </el-cascader>
          </el-form-item>-->
          <el-form-item label="银行代码" prop="bankCode">
            <el-select @change="bankNameChange" v-model="editChildForm.bankCode" placeholder="请选择"
                       filterable style="width:100%">
              <el-option
                v-for="item in banks"
                :key="item.bankName"
                :label="item.bankName"
                :value="item.bankCode">
              </el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="银行名称" prop="interBankNo">
            <el-input readonly="readonly" v-model="editChildForm.bankName" auto-complete="off" placeholder="请选择" @click.native="getUnioLs()"/>
            <!--<el-select v-model="editChildForm.interBankNo" placeholder="请选择" filterable style="width:100%">
              <el-option
                v-for="item in unionnos"
                :key="item.bankName"
                :label="item.bankName"
                :value="item.interBankNo"
              >
              </el-option>
            </el-select>-->
          </el-form-item>
          <el-form-item label="账户号" prop="account">
            <el-input :disabled="accountStatus=='date'" v-model="editChildForm.account" placeholder="账户号"/>
          </el-form-item>
          <el-form-item label="账户名称" prop="accName">
            <el-input readonly v-model="editChildForm.accName" placeholder="账户名称"/>
          </el-form-item>
          <el-form-item label="注册地址" prop="registeAddress">
            <el-input readonly v-model="editChildForm.registeAddress" placeholder="注册地址"/>
          </el-form-item>
          <el-form-item label="联系电话">
            <el-input v-model="editChildForm.bankLinkMobile" placeholder="联系电话"/>
          </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button @click="acclistDialogChildVisible = false">取消</el-button>
          <el-button type="primary" @click="doChildAdd">{{accountStatus == 'edit' ? '新增' : '确定'}}</el-button>
        </div>
      </el-dialog>
    </el-dialog>
    <!-- 新增保理公司对话框 -->
    <el-dialog :visible.sync="editDialogVisible" :title="dialogStatus == 'create' ? '新增保理公司' : '编辑保理公司'" width="900px"
               :close-on-click-modal="false">
      <el-form ref="editForm" :rules="editFormRules" :model="editForm" status-icon label-position="right"
               label-width="140px" style="padding-right: 20px;">
        <div style="display: flex">
          <div style="flex: 1; margin-right: 40px">
            <el-form-item label="公司简称" prop="companyName">
              <el-input v-model="editForm.companyName" placeholder="公司名称"/>
            </el-form-item>
            <el-form-item label="公司全称" prop="fullName">
              <el-input v-model="editForm.fullName" auto-complete="off" placeholder="公司全称"/>
            </el-form-item>
            <el-form-item label="统一社会信用代码" prop="bankAccName">
              <el-input v-model="editForm.bankAccName" placeholder="统一社会信用代码"/>
            </el-form-item>
            <el-form-item label="省城市">
              <el-cascader style="width:100%"
                :options="cities"
                v-model="editForm.provinceAndCity"
                :props="{ expandTrigger: 'hover',value:'label' }"
              ></el-cascader>
              <!--<el-input v-model="editForm.districtCode" auto-complete="off"/>-->
            </el-form-item>
            <el-form-item label="办公地址">
              <el-input v-model="editForm.address" placeholder="办公地址"/>
            </el-form-item>
            <el-form-item label="注册地址" prop="registeAddress">
              <el-input v-model="editForm.registeAddress" placeholder="注册地址"/>
            </el-form-item>
          </div>
          <div style="flex: 1">
            <el-form-item label="联系人" prop="linkMan">
              <el-input v-model="editForm.linkMan" placeholder="联系人"/>
            </el-form-item>
            <el-form-item label="联系人电话(办公)">
              <el-input v-model="editForm.linkPhone" placeholder="联系人电话"/>
            </el-form-item>
            <el-form-item label="手机号" prop="linkMobile">
              <el-input v-model="editForm.linkMobile" placeholder="手机号"/>
            </el-form-item>
            <el-form-item label="电子邮箱" prop="email">
              <el-input v-model="editForm.email" placeholder="电子邮箱"/>
            </el-form-item>
            <el-form-item v-if="dialogStatus == 'create'" label="登录用户" prop="userName">
              <el-input v-model="editForm.userName" placeholder="登录用户"/>
            </el-form-item>
            <el-form-item v-if="dialogStatus == 'create'" label="登录密码" prop="password">
              <el-input v-model="editForm.password" placeholder="登录密码"/>
            </el-form-item>
          </div>
        </div>
        <el-form-item label="备注">
          <el-input type="textarea" v-model="editForm.remark"></el-input>
        </el-form-item>
        <!--<el-form-item label="公司收付款账户列表">
          <el-checkbox-group v-model="editForm.acclist">
            <el-checkbox label="开户银行" name="acclist"></el-checkbox>
            <el-checkbox label="银行联行号" name="acclist"></el-checkbox>
            <el-checkbox label="银行账号" name="acclist"></el-checkbox>
            <el-checkbox label="账号名称" name="acclist"></el-checkbox>
          </el-checkbox-group>
        </el-form-item>-->
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="editDialogVisible = false">取消</el-button>
        <el-button type="primary" @click="doEdit">{{dialogStatus == 'create' ? '新增' : '确定'}}</el-button>
      </div>
    </el-dialog>

    <!--支行弹窗-->
    <el-dialog :visible.sync="UnionDialogVisible" title="选择开户银行名称" :close-on-click-modal="false">
      <div class="filter-container">
        <el-form :inline="true" status-icon label-position="left" class="demo-form-inline"
                 label-width="100px" style="width: 100%; margin-left:50px;">
          <el-form-item label="开户银行名称" prop="account">
            <el-input auto-complete="off" v-model="editForm1.interBankName" placeholder="开户银行名称" style="width:400px" />
          </el-form-item>
          <el-form-item>
            <el-button class="filter-item" type="primary" @click="queryUnio">查询</el-button>
          </el-form-item>
        </el-form>
      </div>
      <el-table v-loading="unionnosLoading" :data="unionnos" element-loading-text="正在查询中。。。" border fit highlight-current-row
                :header-cell-style="{background:'#92D3FF',color:'#fff'}">
        <el-table-column align="left" min-width="100" label="开户银行名称" prop="bankName"/>
        <el-table-column align="center" label="操作" width="200" class-name="small-padding fixed-width">
          <template slot-scope="scope">
            <el-button type="primary" size="mini"
                       @click="choose(scope.row)">选择
            </el-button>
          </template>
        </el-table-column>
      </el-table>
      <pagination v-show="total1>0" :total="total1" :page.sync="editForm1.pageNum" :limit.sync="editForm1.pageSize"
                  @pagination="getUnionnos"/>
    </el-dialog>

  </div>
</template>

<script>
  import {doGet, doPost} from '@/api'
  import {cities} from '@/utils/base'
  import Pagination from '@/components/Pagination' // Secondary package based on el-pagination
  import checkPermission from '@/utils/permission' // 权限判断函数

  export default {
    name: 'factoring',
      al : '',
      pageNum: '',
      pageSize: '',
    components: {Pagination},
    filters: { },
    data() {
      let mobileValidate = (rule, value, callback) => {
        let reg = /^[1][0-9]{10}$/;
        if (value == '') {
          callback(new Error('手机号不允许为空'));
        } else if (!reg.test(value)) {
          callback(new Error('手机号格式不正确'));
        } else {
          callback()
        }
      }
      return {
        list: [],
        total: 0,
          total1: 0,
        listLoading: true,
        listQuery: {
          pageNum: 1,
          pageSize: 20,
        },
        dialogStatus: 'create',
        editForm: {
          companyName: undefined,
          fullName: undefined,
          bankAccName: undefined,
          address: undefined,
          linkMan: undefined,
          linkPhone: undefined,
          linkMobile: undefined,
          provinceAndCity: undefined,
          email: undefined,
          userName: undefined,
          password: undefined,
          remark: undefined,
          registeAddress: undefined
        },
        editFormRules: {
          companyName: [{required: true, message: '公司简称不允许为空', trigger: 'blur'}],
          bankAccName: [{required: true, message: '统一社会信用代码不允许为空', trigger: 'blur'}],
          /*linkMan: [{required: true, message: '联系人不允许为空', trigger: 'blur'}],*/
          /*linkMobile: [{required: true, validator:mobileValidate, trigger: 'blur'}],*/
          userName: [{required: true, message: '登录用户名不允许为空', trigger: 'blur'}],
          password: [{required: true, message: '登录密码不允许为空', trigger: 'blur'}],
          email: [{ type: 'email', message: '请输入正确的邮箱地址', trigger: 'blur' }],
          registeAddress: [{ required: true, message: '注册地址不允许为空', trigger: 'blur' }],
          fullName: [{ required: true, message: '公司全称不允许为空', trigger: 'blur' }]
        },
        editDialogVisible: false,
        cities: cities,
        acclistDialogVisible: false, // 账户列表对话框显示标志
        acclistLoading: false, // 查看账户列表loading标志
        acclist: [], // 账户列表数据
        acclistDialogChildVisible: false, // 增加服务商账户对话框显示标志
        editChildForm: {
          selectedBankInfo: undefined,
          bankCode: undefined,
          interBankNo: undefined,
          account: undefined,
          accName: undefined,
            bankName: undefined,
          registeAddress: undefined,
          bankLinkMobile: undefined
        },
        editChildFormRules: {
          //selectedBankInfo: [{required: true, message: '请选择银行代码', trigger: 'blur'}],
           bankCode: [{required: true, message: '银行代码不允许为空', trigger: 'blur'}],
          interBankNo: [{required: true, message: '银行名称不允许为空', trigger: 'blur'}],
          account: [{required: true, message: '账户号不允许为空', trigger: 'blur'}],
          accName: [{required: true, message: '账户名称不允许为空', trigger: 'blur'}],
          registeAddress: [{required: true, message: '注册地址不允许为空', trigger: 'blur'}]
        },
        id: undefined,
        accountStatus: 'edit',
        bankInfo: [],
          banks: [],
          unionnos: [],
          UnionDialogVisible: false,
          unionnosLoading: true,
          editForm1: {
              pageNum: 1,
              pageSize: 20,
              bankCode: undefined,
              interBankName: undefined
          },
      }
    },
    created() {
      this.getList()
        this.getBanks()
    },
    methods: {
      checkPermission,
        getBanks() {
            doGet('bank.html?op=list', {}).then(response => {
                this.banks = response.rows
            }).catch(() => {
                this.banks = []
            })
        },
        getUnionnos(subankCode, flag) {
            if (typeof(subankCode) == 'string') {
                this.pageNum = 1
                this.pageSize = 20
            } else {
                this.pageNum = subankCode.page
                this.pageSize = subankCode.limit
            }
            this.editForm1 = {
                pageNum: this.pageNum,
                pageSize: this.pageSize,
                bankCode: this.al,
                bankName: this.editForm1.interBankName,
                interBankNo: this.editChildForm.interBankNo,
                flag: flag
                // bankName: this.editChildForm.interBankNo
            }
            this.UnionDialogVisible = true
            this.unionnosLoading = true
            doGet('unionno.html?op=list', this.editForm1).then(response => {
                this.unionnos = response.rows
                this.total1 = response.data.totalcount
                this.unionnosLoading = false
            }).catch(() => {
                this.unionnos = []
                this.total1 = 0
                this.unionnosLoading = false
            })
        },
        bankNameChange(bankCode) {
            this.al = bankCode.substring(0, 3)
            var flag = '1';
            this.getUnionnos(this.al, flag)
            this.editChildForm.interBankNo = undefined
        },
      // 获取公司列表
      getList() {
        this.listLoading = true
        doGet('factoring.html?op=list',this.listQuery).then(response => {
          this.list = response.rows
          this.total = response.data.totalcount
          this.listLoading = false
        }).catch(() => {
          this.list = []
          this.total = 0
          this.listLoading = false
        })
      },

      // 获取账户列表
      getAccList(id) {
        this.acclistLoading = true
        doGet('factoring.html?op=acclist', {id}).then(response => {
          this.acclist = response.rows
          this.acclistLoading = false
        }).catch(() => {
          this.acclist = []
          this.acclistLoading = false
        })
      },

      // 查看公司账户
      handleCheck(row) {
        this.acclist = []
        this.acclistDialogVisible = true
        this.id = row.ID
        this.registeAddress = row.registeAddress
        this.fullName = row.fullName
        this.getAccList(row.ID)
      },

      // 弹出新增公司对话框
      handleAdd() {
        Object.keys(this.editForm).forEach(item => {
          this.editForm[item] = undefined
        })
        this.editDialogVisible = true
        this.dialogStatus = "create"
        this.$nextTick(() => {
          this.$refs['editForm'].clearValidate()
        })
      },

      // 弹出修改公司信息对话框
      handleEdit(row) {
        this.editForm = Object.assign({}, row);
        this.editForm.provinceAndCity = [row.province, row.city]
        this.editIndex = this.list.indexOf(row);
        this.dialogStatus = "update"
        this.editDialogVisible = true
        this.$nextTick(() => {
          this.$refs['editForm'].clearValidate()
        })
      },

      // 提交公司信息
      doEdit() {
        this.$refs['editForm'].validate(valid => {
          if (valid) {
            let sendData = {
              ...this.editForm,
              id: this.editForm.ID,
              province: this.editForm.provinceAndCity ? this.editForm.provinceAndCity[0] : undefined,
              city: this.editForm.provinceAndCity ?  this.editForm.provinceAndCity[1] : undefined
            }
            doGet(`factoring.html?op=${this.dialogStatus == 'create' ? 'add' : 'chg'}`, sendData)
              .then(response => {
                this.getList()
                this.editDialogVisible = false
                this.$notify.success({
                  title: '成功',
                  message: this.dialogStatus == 'create' ? '添加保理公司成功' : '修改公司信息成功'
                })
              })
              .catch(response => {
                this.$notify.error({
                  title: '失败',
                  message: response.message
                })
              })
          }
        })
      },

      // 删除公司
      doDelete(row) {
        doGet("factoring.html?op=del", {...row, id: row.ID})
          .then(response => {
            this.$notify.success({
              title: '成功',
              message: '删除保理公司成功'
            })
            const index = this.list.indexOf(row)
            this.list.splice(index, 1)
            this.$refs[`popover-` + row.ID].doClose()
            this.total--
          })
          .catch(response => {
            this.$notify.error({
              title: '失败',
              message: response.message
            })
          })
      },

      // 弹出新增公司账户对话框
      handleChildAdd() {
        if (!this.bankInfo.length) {
          this.getBankCodeInfo()
        }
        Object.keys(this.editChildForm).forEach(item => {
          this.editChildForm[item] = undefined
        })
        this.accountStatus = "edit"
        this.editChildForm.registeAddress = this.registeAddress
        this.editChildForm.accName = this.fullName
        this.acclistDialogChildVisible = true
        this.$nextTick(() => {
          this.$refs['editChildForm'].clearValidate()
        })
      },

      // 获取银行代码信息
      getBankCodeInfo() {
        doGet('bank.html?op=list').then(response => {
          this.bankInfo = response.rows.map(item => {
            return {
              value: item.bankCode,
              label: item.bankName,
              children: []
            }
          })
        }).catch(() => {
          this.bankInfo = []
        })
      },

      // 银行代码第一级选择触发
      handleItemChange(val) {
        let bankArr = this.bankInfo.filter(item => {
          return item.value == val
        })[0]
        if (!bankArr.children.length) {
          doGet('unionno.html?op=list', {bankName:　bankArr.label}).then(response => {
            this.bankInfo.forEach(item => {
              if (item.value == val) {
                item.children = response.rows.map(items => {
                  return {
                    value: items.interBankNo,
                    label: items.bankName
                  }
                })
              }
            })
          }).catch(() => {
            // this.bankInfo = []
          })
        }
      },

      // 弹出修改公司账户
      ChandEdit(row) {
        this.editChildForm = {
          ...row,
          bankName: row.interBankName
        };
        this.accountStatus = "date"
        /*this.ChildIndex = this.acclist.indexOf(row);*/
        this.acclistDialogChildVisible = true
        this.$nextTick(() => {
          this.$refs['editChildForm'].clearValidate()
        })
      },

      // 提交新增公司账户信息
      doChildAdd() {
        this.$refs['editChildForm'].validate(valid => {
          if (valid) {
            this.editChildForm = {
              id: this.id,
              /*bankCode: this.editChildForm.selectedBankInfo[0],
              interBankNo: this.editChildForm.selectedBankInfo[1],*/
              bankCode: this.editChildForm.bankCode,
              interBankNo: this.editChildForm.interBankNo,
              account: this.editChildForm.account,
              accName: this.editChildForm.accName,
              bankLinkMobile: this.editChildForm.bankLinkMobile,
              registeAddress: this.registeAddress,
              fullName: this.fullName
            }
            doGet(`factoring.html?op=${this.accountStatus == 'edit' ? 'accadd' : 'accUpdate'}`, this.editChildForm)
              .then(response => {
                this.getAccList(this.id)
                this.acclistDialogChildVisible = false
                this.$notify.success({
                  title: '成功',
                  message: this.accountStatus == 'edit' ? '添加账户成功' : '修改账户信息成功'
                })
              })
              .catch(response => {
                this.acclistDialogChildVisible = false
                this.$notify.error({
                  title: '失败',
                  message: response.message
                })
              })
            /*doGet('factoring.html?op=accadd', setData)
              .then(response => {
                this.getAccList(this.id)
                this.acclistDialogChildVisible = false
                this.$notify.success({
                  title: '成功',
                  message: '添加公司账户成功'
                })
              })
              .catch(response => {
                this.acclistDialogChildVisible = false
                this.$notify.error({
                  title: '失败',
                  message: response.message
                })
              })*/
          }
        })
      },

      // 删除公司账户
      doChildDelete(row) {
        doGet("factoring.html?op=accdel", row)
          .then(response => {
            this.$notify.success({
              title: '成功',
              message: '删除公司账户成功'
            })
            const index = this.acclist.indexOf(row)
            this.acclist.splice(index, 1)
            this.$refs[`popover-` + row.bankCode].doClose()
          })
          .catch(response => {
            this.$notify.error({
              title: '失败',
              message: response.message
            })
          })
      },
        /**查询银行名称*/
        queryUnio() {
            this.UnionDialogVisible = true
            this.pageNum = this.pageNum
            this.pageSize = this.pageSize
            this.getUnionnos(this.al, '2')
        },
        getUnioLs() {
        if(this.al == null){
            alert('请选择银行代码')
          } else {
            this.UnionDialogVisible = true
            this.pageNum = this.pageNum
            this.pageSize = this.pageSize
            this.getUnionnos(this.al, '1')
          }
        },
        /** 选择分行*/
        choose(row) {
            this.editChildForm.interBankNo = row.interBankNo
            this.editChildForm.bankName = row.bankName
            this.editForm1.interBankName = row.bankName
            this.UnionDialogVisible = false
        },
    }
  }
</script>
